home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / programming / other / scm / slib / synchk.scm < prev    next >
Text File  |  1999-04-19  |  4KB  |  105 lines

  1. ;;; "synchk.scm" Syntax Checking            -*-Scheme-*-
  2. ;;; Copyright (c) 1989-91 Massachusetts Institute of Technology
  3. ;;;
  4. ;;; This material was developed by the Scheme project at the
  5. ;;; Massachusetts Institute of Technology, Department of Electrical
  6. ;;; Engineering and Computer Science.  Permission to copy this
  7. ;;; software, to redistribute it, and to use it for any purpose is
  8. ;;; granted, subject to the following restrictions and understandings.
  9. ;;;
  10. ;;; 1. Any copy made of this software must include this copyright
  11. ;;; notice in full.
  12. ;;;
  13. ;;; 2. Users of this software agree to make their best efforts (a) to
  14. ;;; return to the MIT Scheme project any improvements or extensions
  15. ;;; that they make, so that these may be included in future releases;
  16. ;;; and (b) to inform MIT of noteworthy uses of this software.
  17. ;;;
  18. ;;; 3. All materials developed as a consequence of the use of this
  19. ;;; software shall duly acknowledge such use, in accordance with the
  20. ;;; usual standards of acknowledging credit in academic research.
  21. ;;;
  22. ;;; 4. MIT has made no warrantee or representation that the operation
  23. ;;; of this software will be error-free, and MIT is under no
  24. ;;; obligation to provide any services, by way of maintenance, update,
  25. ;;; or otherwise.
  26. ;;;
  27. ;;; 5. In conjunction with products arising from the use of this
  28. ;;; material, there shall be no use of the name of the Massachusetts
  29. ;;; Institute of Technology nor of any adaptation thereof in any
  30. ;;; advertising, promotional, or sales literature without prior
  31. ;;; written consent from MIT in each case.
  32.  
  33. ;;;; Syntax Checking
  34. ;;; written by Alan Bawden
  35. ;;; modified by Chris Hanson
  36.  
  37. (define (syntax-check pattern form)
  38.   (if (not (syntax-match? (cdr pattern) (cdr form)))
  39.       (syntax-error "ill-formed special form" form)))
  40.  
  41. (define (ill-formed-syntax form)
  42.   (syntax-error "ill-formed special form" form))
  43.  
  44. (define (syntax-match? pattern object)
  45.   (let ((match-error
  46.      (lambda ()
  47.        (impl-error "ill-formed pattern" pattern))))
  48.     (cond ((symbol? pattern)
  49.        (case pattern
  50.          ((IDENTIFIER) (identifier? object))
  51.          ((DATUM EXPRESSION FORM) #t)
  52.          ((R4RS-BVL)
  53.           (let loop ((seen '()) (object object))
  54.         (or (null? object)
  55.             (if (identifier? object)
  56.             (not (memq object seen))
  57.             (and (pair? object)
  58.                  (identifier? (car object))
  59.                  (not (memq (car object) seen))
  60.                  (loop (cons (car object) seen) (cdr object)))))))
  61.          ((MIT-BVL) (lambda-list? object))
  62.          (else (match-error))))
  63.       ((pair? pattern)
  64.        (case (car pattern)
  65.          ((*)
  66.           (if (pair? (cdr pattern))
  67.           (let ((head (cadr pattern))
  68.             (tail (cddr pattern)))
  69.             (let loop ((object object))
  70.               (or (and (pair? object)
  71.                    (syntax-match? head (car object))
  72.                    (loop (cdr object)))
  73.               (syntax-match? tail object))))
  74.           (match-error)))
  75.          ((+)
  76.           (if (pair? (cdr pattern))
  77.           (let ((head (cadr pattern))
  78.             (tail (cddr pattern)))
  79.             (and (pair? object)
  80.              (syntax-match? head (car object))
  81.              (let loop ((object (cdr object)))
  82.                (or (and (pair? object)
  83.                     (syntax-match? head (car object))
  84.                     (loop (cdr object)))
  85.                    (syntax-match? tail object)))))
  86.           (match-error)))
  87.          ((?)
  88.           (if (pair? (cdr pattern))
  89.           (or (and (pair? object)
  90.                (syntax-match? (cadr pattern) (car object))
  91.                (syntax-match? (cddr pattern) (cdr object)))
  92.               (syntax-match? (cddr pattern) object))
  93.           (match-error)))
  94.          ((QUOTE)
  95.           (if (and (pair? (cdr pattern))
  96.                (null? (cddr pattern)))
  97.           (eqv? (cadr pattern) object)
  98.           (match-error)))
  99.          (else
  100.           (and (pair? object)
  101.            (syntax-match? (car pattern) (car object))
  102.            (syntax-match? (cdr pattern) (cdr object))))))
  103.       (else
  104.        (eqv? pattern object)))))
  105.